package com.example.sssss;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private Button btnLogin;
    private Button btnRegister;
    private EditText etUsername;
    private EditText etPassword;
    private AlertDialog progressDialog;

    // 预设的正确用户名和密码（实际项目中会从服务器验证）
    private static final String CORRECT_USERNAME = "admin";
    private static final String CORRECT_PASSWORD = "123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 处理窗口边距
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化视图
        initViews();

        // 设置点击事件
        setClickListeners();
    }

    private void initViews() {
        btnLogin = findViewById(R.id.btnLogin);
        btnRegister = findViewById(R.id.btnRegister);
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);

        // 创建进度对话框
        progressDialog = new AlertDialog.Builder(this)
                .setView(R.layout.progress_dialog)
                .setCancelable(false)
                .create();
    }

    private void setClickListeners() {
        // 登录按钮点击事件
        btnLogin.setOnClickListener(v -> attemptLogin());

        // 注册按钮点击事件
        btnRegister.setOnClickListener(v -> showLoading("正在注册"));
    }

    /**
     * 尝试登录，包含输入验证和账号密码验证
     */
    private void attemptLogin() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        // 输入验证
        if (TextUtils.isEmpty(username)) {
            showError(etUsername, "请输入用户名");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            showError(etPassword, "请输入密码");
            return;
        }

        if (password.length() < 6) {
            showError(etPassword, "密码长度不能少于6位");
            return;
        }

        // 验证用户名和密码
        if (CORRECT_USERNAME.equals(username) && CORRECT_PASSWORD.equals(password)) {
            // 验证通过，显示加载进度
            showLoading("正在登录");
        } else {
            // 验证失败，显示错误提示
            Toast.makeText(this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示输入错误提示
     */
    private void showError(EditText editText, String message) {
        editText.setError(message);
        editText.requestFocus(); // 获取焦点，方便用户修改
    }

    /**
     * 显示加载进度条
     * @param message 显示的消息
     */
    private void showLoading(String message) {
        // 设置进度条消息
        if (progressDialog.findViewById(R.id.tvLoadingMessage) != null) {
            ((TextView) progressDialog.findViewById(R.id.tvLoadingMessage))
                    .setText(message);
        }

        // 显示进度条
        progressDialog.show();

        // 3秒后隐藏进度条并显示成功提示
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

            // 清除输入框内容（可选）
            etUsername.setText("");
            etPassword.setText("");
        }, 3000);
    }
}
